﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using AccessCommon;
using BackboneTest.Models;

namespace BackboneTest.DAL
{
    public partial class GuestbookDAL
    {
       #region 成员方法
    
       /// <summary>
       /// 分页列表
       /// </summary>
       /// <param name="GuestbookItem"></param>
       /// <param name="pageSize"></param>
       /// <param name="index"></param>
       /// <param name="Total"></param>
       /// <returns></returns>
       public DataTable GetList(GuestbookItem searchItem, int pageSize, int index, out int Total)
       {
           StringBuilder strSql = new StringBuilder();
           strSql.Append("select GuestbookID,Contents,AddTime,UserID from Guestbook   where 1=1  ");
        
           string order = " GuestbookID ";
           return mydb.GetPrintTable(strSql.ToString(), order, "*", pageSize, index, out Total);
        }
        /// <summary>
        /// 导入Excel数据
        /// </summary>
        /// <param name="dtExcel"></param>
        /// <returns></returns>
        public bool ExcelToDataBase(DataTable dtExcel)
        {
            mydb.BeginTransaction();
            try
            {
                dtExcel.Rows.RemoveAt(0);
                int colcount = dtExcel.Columns.Count;
                int rowcount = dtExcel.Rows.Count;
            
            
                foreach (DataRow dr in dtExcel.Rows)
                {
                    if (dr[0].ToString() != "")
                    {
                        Guestbook model = new Guestbook();
        
                        model.Contents = dr["Contents"].ToString();
                        model.AddTime = ConvertClass.ToDateTime(dr["AddTime"].ToString());
                        model.UserID = ConvertClass.ToInt32(dr["UserID"].ToString());
                         if (Add(model)==-1)   //插入新记录
                        {
                            mydb.Rollback();
                            return false;
                        }
            
                    }
                }
                mydb.Commit();
                return true;
            
            }
            catch (Exception ex)
            {
                string strex = ex.ToString();
                mydb.Rollback();
                return false;
            }
            finally
            {
                mydb.EndTransaction();
            
            }
        }
    
        #endregion  成员方法
    
    }  
    
}

